package com.flying.date.jdk8;

import org.junit.Test;

import java.time.*;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.temporal.*;
import java.util.*;

/**
 * @author 刘鹏飞
 * @ClassName DateTimeDemo
 * @Date 2024/12/21 19:13
 * @Created by 刘鹏飞
 * @Description java8的日期时间API
 */
public class DateTimeDemo {

    /*
    java8之前日期时间API的局限性
    可变性:像日期时间这样的尅应该是不可变的.
    偏移性: Date中的年份是从1900开始的,而月份都是从0开始的.
    格式化: 格式化只对Date有用,Calender则不行
    此外,他们也不是线程安全的;不能处理闰秒等

     */


    @Test
    public void test1(){

        // calendar的可变性

        Calendar calendar = Calendar.getInstance();

        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));  // 21

        calendar.set(Calendar.DAY_OF_MONTH,6);

        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));  // 6


        // 偏移性

        Date date = new Date(0, Calendar.OCTOBER, 14);  // Sun Oct 14 00:00:00 CST 1900
        Date date1 = new Date(2024, Calendar.OCTOBER, 14);  // Sun Oct 14 00:00:00 CST 1900
        System.out.println(date);
        System.out.println(date1);   // Tue Oct 14 00:00:00 CST 3924


    }


    /*
    Java8中的API :   LocalDate /  LocalTime /   LocalDateTime
     */

    /**
     * 2种实例化方式
     */
    @Test
    public void test2(){
        //方式1: now(): 获取当前日期和时间的实例

        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println("localDate = " + localDate);  // 2024-12-21
        System.out.println("localTime = " + localTime);  // 19:40:55.482138100
        System.out.println("localDateTime = " + localDateTime);  //   localDateTime = 2024-12-21T19:40:55.482138100


        // 方式2:of(): 获取指定的日期/时间对应的实例

        LocalDate localDate1 = LocalDate.of(2024, 12, 1);
        LocalTime localTime1 = LocalTime.of(20, 30,20);
        LocalDateTime localDateTime1 = LocalDateTime.of(2016, Month.DECEMBER, 20, 6, 20, 45);

        System.out.println("localDateTime1 = " + localDateTime1);
        System.out.println("localTime1 = " + localTime1);
        System.out.println("localDate1 = " + localDate1);
    }



    //常用方法
    @Test
    public void test3(){

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);
        // getXxx():

        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getChronology());  // ISO


        // withXxx():体验不变性
        // 修改时间时,不是直接对原对象修改,而是返回一个新的对象
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(2);
        System.out.println("localDateTime1 = " + localDateTime1);
        LocalDateTime localDateTime2 = localDateTime.withMinute(22);
        System.out.println("localDateTime2 = " + localDateTime2);


        //plusXxx(): 对时间进行 + 运算
        LocalDateTime localDateTime3 = localDateTime.plusMonths(2);
        System.out.println("localDateTime = " + localDateTime);    // localDateTime = 2024-12-21T20:30:46.665923800
        System.out.println("localDateTime3 = " + localDateTime3);  // localDateTime3 = 2025-02-21T20:30:46.665923800



        //minusXxx: 对时间进行 - 运算
        LocalDateTime localDateTime4 = localDateTime.minusMonths(4);
        System.out.println("localDateTime4 = " + localDateTime4);


        // 判断是否是闰年

        System.out.println(LocalDate.now().isLeapYear());


    }


    // Java8中的API  :  Instant --- 瞬时 比较类似于 Date

    @Test
    public void test4(){

        // Instant.now(): 实例化
        Instant instant = Instant.now();
        System.out.println("instant = " + instant);

        OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(2));
        System.out.println(instant);
        System.out.println(instant1);

        // 已指定的时间戳创建瞬时对象
        Instant instant2 = Instant.ofEpochMilli(276286826L);
        System.out.println("instant2 = " + instant2);


        // 将瞬时对象转换为时间戳
        long epochMilli = instant2.toEpochMilli();
        System.out.println("epochMilli = " + epochMilli);
        System.out.println(new Date(276286826L).getTime());


    }


    /*
     JDK8 中的日期格式化API: DateTimeFormatter
     */

    @Test
    public void test5(){

        // 1. 创建格式化器

        // 默认格式:
        DateTimeFormatter dtf1 = DateTimeFormatter.ISO_DATE;  // 2011-12-03  ISO_DATE : 系统内置格式化器



        // 指定格式
        // 1. 通过ofPatten()
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");  // hh: 12小时制 HH: 24小时制

        // 2. 通过 DateStyle
        DateTimeFormatter dtf3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        DateTimeFormatter dtf4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        DateTimeFormatter dtf5 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        DateTimeFormatter dtf6 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);




        // 格式化:  日期 --> 字符串

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(dtf1.format(localDateTime));
        System.out.println(dtf2.format(localDateTime));
        System.out.println(dtf3.format(localDateTime));
        System.out.println(dtf4.format(localDateTime));
        System.out.println(dtf5.format(localDateTime));
        System.out.println(dtf6.format(localDateTime));



        // 解析 :  字符串  --> 时间
        // 方式1: 调用解释器的parse(str)方法,返回一个时间访问器,通过
        TemporalAccessor temporalAccessor = dtf2.parse("2024-12-21  21:13:12");

        LocalDateTime localDateTime1 = LocalDateTime.from(temporalAccessor);
        System.out.println("localDateTime1 = " + localDateTime1);
        LocalDate localDate = LocalDate.from(temporalAccessor);
        System.out.println("localDate = " + localDate);

        // 方式2: 调用日期时间API的parse(str,formatter) 方法,传入一个字符串和一个解释器
        //   String  -->  LocalDateTime :  需要保证DateTimeFormatter 是24小时制 调用LocalDateTime.pase方法,传入一个字符串,和一个解释器
        LocalDateTime localDateTime2= LocalDateTime.parse("2011/04/12 10:11:33",dtf2);
        System.out.println(localDateTime2);
    }



}
